package com.example.liulongjin;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {

    private RecyclerView mainRecyclerView;
    private AppCompatTextView queryTextView;

    private final ArrayList<IpBean> arrayList = new ArrayList<>();
    private final MainAdapter adapter = new MainAdapter(arrayList);
    private final String[] ips = {"139.203.134.244", "223.104.90.8", "112.10.87.52", "122.238.62.125", "36.48.78.60",
            "112.97.213.185", "125.66.79.120", "116.232.64.29", "115.193.181.151", "221.192.178.10", "113.109.118.151",
            "101.93.65.57", "60.23.118.129", "8.135.18.242", "1.189.204.105"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //实例化控件
        setContentView(R.layout.activity_main);
        mainRecyclerView = findViewById(R.id.mainRecyclerView);
        queryTextView = findViewById(R.id.queryTextView);
        //初始化数据
        for (int i = 0; i < ips.length; i++) {
            IpBean bean = new IpBean();
            bean.setIp(ips[i]);
            arrayList.add(bean);
        }
        mainRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mainRecyclerView.setAdapter(adapter);
        //初始化事件
        adapter.setOnItemClickListener(new MainAdapter.OnItemClickListener() {
            @Override
            public void onQuery(int position, IpBean bean) {
                OkGo.<String>get("http://whois.pconline.com.cn/ipJson.jsp?ip=" + bean.getIp() + "&json=true").execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        IpBean ipBean = new Gson().fromJson(response.body(), IpBean.class);
                        arrayList.remove(position);
                        arrayList.add(position, ipBean);
                        adapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onClick(int position, IpBean bean) {
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                intent.putExtra("ip", bean.getIp());
                startActivity(intent);
            }
        });
        queryTextView.setOnClickListener(view -> {
            for (int i = 0; i < arrayList.size(); i++) {
                final int position = i;
                arrayList.get(position).setAddr("查询中...");
                adapter.notifyItemChanged(position);
                OkGo.<String>get("http://whois.pconline.com.cn/ipJson.jsp?ip=" + arrayList.get(position).getIp() + "&json=true")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                IpBean ipBean = new Gson().fromJson(response.body(), IpBean.class);
                                arrayList.remove(position);
                                arrayList.add(position, ipBean);
                                adapter.notifyDataSetChanged();
                            }
                        });
            }
        });

    }

}
